package com.hung.hashMap;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author hungki
 */
public class EndLessLoopDemo extends Thread {
    static HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(2);
    static AtomicInteger at = new AtomicInteger();
    static CountDownLatch countDownLatch= new CountDownLatch(1);

    public static void t1(){
        MyThead myThead1 = new MyThead(countDownLatch);
        MyThead myThead2 = new MyThead(countDownLatch);
        MyThead myThead3 = new MyThead(countDownLatch);
        MyThead myThead4 = new MyThead(countDownLatch);
        MyThead myThead5 = new MyThead(countDownLatch);
        MyThead myThead6 = new MyThead(countDownLatch);
        MyThead myThead7 = new MyThead(countDownLatch);
        MyThead myThead8 = new MyThead(countDownLatch);
        MyThead myThead9 = new MyThead(countDownLatch);
        MyThead myThead10 = new MyThead(countDownLatch);
        MyThead myThead11 = new MyThead(countDownLatch);

        myThead1.start();
        myThead2.start();
        myThead3.start();
        myThead4.start();
        myThead5.start();
        myThead6.start();
        myThead7.start();
        myThead8.start();
        myThead9.start();
        myThead10.start();
        myThead11.start();
        countDownLatch.countDown();
    }
    public static void  t2(){
        MyThead[] mts = {new MyThead(countDownLatch),new MyThead(countDownLatch)};
        MyThead newMyThread = mts[0];
        if(newMyThread !=null){
            mts[0]=null;
        }
        System.out.println("newMyThread = " + newMyThread);
    }
    public static void main(String[] args) {
        t2();
    }
    static class MyThead extends Thread{
        private CountDownLatch countDownLatch;
        MyThead(CountDownLatch countDownLatch){
            this.countDownLatch = countDownLatch;
        }
          @Override
          public void run() {
              try {
                  countDownLatch.await();
              } catch (InterruptedException e) {

              }
              while (at.get() < 1000100) {
                  map.put(at.get(), at.get());
                  at.incrementAndGet();
              }
          }
    }
}